//
//  SIXPulishViewController.m
//  微博-发布
//
//  Created by key on 16/5/20.
//  Copyright © 2016年 key. All rights reserved.
//

#import "SIXPulishViewController.h"
#import "MJExtension.h"
#import "SIXPulishModel.h"
#import "SIXPulishCell.h"
//#import "SIXWriteViewController.h"
#import "SIXWriteViewNavController.h"
#import "SIXComposeViewController.h"
#define kcellW 70
#define kcellH 100
@interface SIXPulishViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *addImg;
@property (weak, nonatomic) IBOutlet UIView *contentView;
/**bool*/
@property(assign,nonatomic)BOOL isAddImg;
/**models*/
@property(strong,nonatomic)NSArray *modelsArray;
/**index*/
@property(assign,nonatomic)NSInteger index;
/**NSTimer*/
@property(strong,nonatomic)NSTimer *timer;
@end

@implementation SIXPulishViewController

#pragma mark - lazy
- (NSArray *)modelsArray{
    if (!_modelsArray) {
        _modelsArray = [SIXPulishModel mj_objectArrayWithFilename:@"Pulish.plist"];
    }
    return _modelsArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化ContentView
    [self setUpContentView];
    //初始化全局索引
    self.index = 0;
    //控制 "+" 按钮的旋转位置
    self.isAddImg = YES;
   
}
#pragma mark - 初始化ContentView(九宫格)
- (void)setUpContentView{
    
    for (int i=0; i<6; i++) {
        
        CGFloat cellW = 70;
        CGFloat cellH = 100;
        //一行的列数
        NSUInteger cols = 3;
        //每一列之间的间距
        CGFloat colMargin = (self.contentView.frame.size.width-cellW*cols)/(cols-1);
        // 每一行之间的间距
        CGFloat rowMargin = 30;
        // 创建一个父控件（整体：存放图片和文字）
        SIXPulishCell *pulishCell = [SIXPulishCell pulish];
        // 索引
        NSUInteger index = i;
        pulishCell.model = self.modelsArray[index];
        // x值
        NSUInteger col = index%cols;
        CGFloat cellX = col*(cellW+colMargin);
        // y值
        NSUInteger row = index/cols;
        CGFloat cellY = row*(cellH+rowMargin);
        // 在pulishCell的XY坐标都决定了之后,确定pulishCell的frame
        pulishCell.frame = CGRectMake(cellX, cellY, cellW, cellH);
        //为pulishCell 里面的按钮添加响应
        for (UIView *view in pulishCell.subviews) {
            if ([view isKindOfClass:[UIButton class]]) {
                view.tag = index*10;
                UIButton *btn = (UIButton *)view;
                [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            }
        }
        // 添加控件
        [self.contentView addSubview:pulishCell];
        
        //当pulishCell被添加到contentView后对其做一次transform,使控件移动到屏幕底部
        pulishCell.transform = CGAffineTransformTranslate(pulishCell.transform, 0, self.contentView.bounds.size.height+kcellH);
    }
    
}

- (void)btnClick:(UIButton *)btn{
    if (btn.tag==0) {
        SIXComposeViewController *writeVC = [[SIXComposeViewController alloc] init];
//        SIXWriteViewController *writeVC = [[SIXWriteViewController alloc] init];
//        writeVC.view.backgroundColor = [UIColor greenColor];
        SIXWriteViewNavController *writeNVC = [[SIXWriteViewNavController alloc] initWithRootViewController:writeVC];
       [self presentViewController:writeNVC animated:YES completion:nil];
        
        
    }
    else if(btn.tag == 10){
        NSLog(@"10");
    }
    else{
        NSLog(@"other");
    }
        
}

//在控制器的View的子控件布局完毕后自动调用,创建定时器并重复执行timeChange方法
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
    self.timer = timer;

}


#pragma mark - 根据 "+" 按钮的旋转位置,来确定执行按钮上升动画,还是按钮下落动画
- (void)timeChange{
    //如果 "+" 按钮在初始位置(正十字),执行上升动画
    if (self.isAddImg == YES) {
        //根据索引,取出contentView的子控件
        SIXPulishCell *cell = self.contentView.subviews[self.index];
        
        //执行动画,让原来移动到底部的控件复原
        [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{
            cell.transform = CGAffineTransformIdentity;
        } completion:nil];
        //索引自增
        self.index++;
        //判断索引是否越界
        if (self.index==self.modelsArray.count) {
            //停止定时器
            [self.timer invalidate];
            //让索引最大化(在不越界的情况下)
            self.index=self.modelsArray.count-1;
            return;
        }

    }
    else{//执行下降动画
        //根据索引,取出contentView的子控件
        SIXPulishCell *cell = self.contentView.subviews[self.index];
        //执行动画,让控件移动到底部
        [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{
            cell.transform = CGAffineTransformTranslate(cell.transform, 0, self.contentView.bounds.size.height+kcellH);
        } completion:nil];
        //索引自减
        self.index--;
        //判断索引是否越界
        if (self.index==-1) {
            //停止定时器
            [self.timer invalidate];
            //让索引为0
            self.index=0;
            return;
        }
        
    
    }
}
#pragma mark - 点击按钮 改变 "+" 按钮旋转位置
- (IBAction)click:(id)sender {
    
    [self addImgChange];
 
    
    
}
#pragma mark - 改变 "+" 按钮旋转位置
- (void)addImgChange{
    if (self.isAddImg) {
        
        
        [UIView animateWithDuration:0.5 animations:^{
            self.addImg.transform = CGAffineTransformMakeRotation(M_PI_4);
        } completion:^(BOOL finished) {
            self.isAddImg = NO;
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
        
    }
    else{
        [UIView animateWithDuration:0.5 animations:^{
            self.addImg.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            self.isAddImg = YES;
        }];
        
        
    }
}




@end
